package com.fingard.util.secret.salary;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.fingard.text.StringHelper;

/**
 * Created by F on 2019/1/6.
 */
public class FieldHiddenUtil {
    /**
     * 对报文msg中的fields字段中的内容用hiddle表示符进行替换
     * @param msg
     * @param fields
     * @return
     */
    public static String amountHiddle(String msg,String[] fields){
        if(fields!=null&&fields.length!=0){
            String regex="";
            for(String field:fields){
                regex =regex+"((?<=<"+field+">)(<!\\[CDATA\\[)?\\d+\\.?\\d{0,2}(\\]\\]>)?(?=</"+field+">))|";
            }
            regex = regex.substring(0,regex.length()-1);
//            Pattern pattern = Pattern.compile("((?<=<pybamt>)\\d+\\.?\\d{0,2}(?=</pybamt>))|((?<=<pybnum>)\\d+\\.?\\d{0,2}(?=</pybnum>))");
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(msg);
            matcher.find();
            return matcher.replaceAll("***.**");
        }
        return null;
    }

    public static String wordHiddle(String msg,String[] fields){
        if(fields!=null&&fields.length!=0){
            String regex="";
            for(String field:fields){
                regex =regex+"((?<=<"+field+">)(<!\\[CDATA\\[)?.*(\\]\\]>)?(?=</"+field+">))|";
            }
            regex = regex.substring(0,regex.length()-1);
//            Pattern pattern = Pattern.compile("((?<=<pybamt>)\\d+\\.?\\d{0,2}(?=</pybamt>))|((?<=<pybnum>)\\d+\\.?\\d{0,2}(?=</pybnum>))");
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(msg);
            matcher.find();
            return matcher.replaceAll("********");
        }
        return null;
    }

    public static void main(String[] args) {
        String ss = "<DecryptKey>aaaaa</DecryptKey>";
        System.out.println(wordHiddle(ss,new String[]{"DecryptKey"}));
    }
}
